package com.twotwoshop.service.goods.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.twotwoshop.domin.goods.pojo.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface ProductMapper extends BaseMapper<Product> {


    /**
     * 批量更新商品浏览量
     * @param updates 需要更新的商品列表
     */
    @Update("<script>" +
            "UPDATE product " +
            "SET views = CASE id " +
            "<foreach collection='list' item='item'>" +
            "WHEN #{item.id} THEN #{item.views} " +
            "</foreach>" +
            "END, " +
            "updated_time = NOW() " +  // 自动更新更新时间
            "WHERE id IN " +
            "<foreach collection='list' item='item' open='(' separator=',' close=')'>" +
            "#{item.id}" +
            "</foreach>" +
            "</script>")
    void batchUpdateViewCount(List<Product> updates);
}